<?php

// 本类由系统自动生成，仅供测试用途
class AdminNewsAction extends Action {

    public function xwfb() {
        $sort = M("sort");
        $xysort = M("xysort");
        $sortlist = $sort->where('sort=0')->select();
        $xylist = $xysort->select();
        $this->assign('sortlist', $sortlist);
        $this->assign('xylist', $xylist);
        $this->display();
    }

    public function xwgl() {

        function cxsort($id) {
            $sort = M("sort");
            $sortinfo = $sort->where('id=' . $id)->find();
            return $sortinfo['name'];
        }

        function cxhy($hy) {
            $xysort = M("xysort");
            $xysortinfo = $xysort->where('id=' . $hy)->find();
            return $xysortinfo['name'];
        }

        import("ORG.Util.Page");
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $news = M("news");
            $sort = M("sort");
            $xysort = M("xysort");
            $keytitle = $_POST['title'];
            $sortlist = $sort->select();
            $xylist = $xysort->select();
            $this->assign('sortlist', $sortlist);
            $this->assign('xylist', $xylist);
            $map['shei'] = array('neq', '3');
            $map['title'] = array('LIKE', '%' . $keytitle . '%');
            $count = $news->where($map)->count();
            $Page = new Page($count, 10);
            $list = $news->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
//        $Page->parameter = 'id=1';
            // 设置分页显示Ï
            $Page->setConfig('header', '条数据');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $page = $Page->show();
            $this->assign("page", $page);
            $this->assign("list", $list);
            $this->assign('id', $id);
            $this->display();
        } else {
            $news = M("news");
            $sort = M("sort");
            $xysort = M("xysort");
            $sortlist = $sort->select();
            $xylist = $xysort->select();
            $this->assign('sortlist', $sortlist);
            $this->assign('xylist', $xylist);
            $count = $news->where('shei!=3')->count();
            $Page = new Page($count, 10);
            $list = $news->where('shei!=3')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
//        $Page->parameter = 'id=1';
            // 设置分页显示Ï
            $Page->setConfig('header', '条数据');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $page = $Page->show();
            $this->assign("page", $page);
            $this->assign("list", $list);
            $this->assign('id', $id);
            $this->display();
        }
    }

    public function shei($id) {
        $news = M("news");
        $data['shei'] = 1;
        $result = $news->where('id=' . $id)->save($data);
        if ($result) {
            echo "<script type='text/javascript' >alert('审核成功');window.location.href = 'xwgl';</script>";
        } else {
            echo "<script type='text/javascript' >alert('审核失败,请稍后在试!');window.location.href = 'xwgl';</script>";
        }
    }

    public function deletenews($id) {
        $news = M("news");
        $data['shei'] = 3;
        $result = $news->where('id=' . $id)->save($data);
        if ($result) {
            echo "<script type='text/javascript' >alert('删除成功');window.location.href = 'xwgl';</script>";
        } else {
            echo "<script type='text/javascript' >alert('删除失败,请稍后在试!');window.location.href = 'xwgl';</script>";
        }
    }

    public function editnews($id) {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $news = M("news"); // 实例化User对象
           if (!empty($_FILES['photo']['name'])) {
                import('ORG.Net.UploadFile');
                $upload = new UploadFile(); // 实例化上传类
                $upload->maxSize = 3145728; // 设置附件上传大小
                $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
                $upload->savePath = './Public/upload/'; // 设置附件上传目录
                if (!$upload->upload()) {// 上传错误提示错误信息
                    $this->error($upload->getErrorMsg());
                } else {// 上传成功 获取上传文件信息
                    $info = $upload->getUploadFileInfo();
                }
                // 保存表单数据 包括附件数据
                $data['logo_img'] = $info[0]['savename']; // 保存上传的照片根据需要自行组装
            }
            $id = $_POST['id'];
            $data['title'] = $_POST['title'];
            $data['content'] = $_POST['content'];
            $data['intro'] = $_POST['intro'];
            $data['editor'] = $_POST['editor'];
            $data['originate'] = $_POST['originate'];
            $data['flag'] = $_POST['flag'];
            $data['shei'] = $_POST['shei'];
            $data['hy'] = $_POST['hy'];
            $data['sort'] = $_POST['sort'];
            $data['time'] = date('Y-m-d H:i:s');
            $news->where('id=' . $id)->save($data); // 写入用户数据到数据库
            echo "<script type='text/javascript' >alert('修改成功');window.location.href = 'xwgl';</script>";
        } else {
            $sort = M("sort");
            $xysort = M("xysort");
            $news = M("news");
            $newsinfo = $news->where('id=' . $id)->find();
            $sortlist = $sort->select();
            $xylist = $xysort->select();
            $this->assign('newsinfo', $newsinfo);
            $this->assign('sortlist', $sortlist);
            $this->assign('xylist', $xylist);
            $this->display();
        }
    }

    public function addnews() {
     	$news = M("news"); // 实例化User对象
        $news->create(); // 创建数据对象
	if (!empty($_FILES['photo']['name'])) {
            import('ORG.Net.UploadFile');
            $upload = new UploadFile(); // 实例化上传类
            $upload->maxSize = 3145728; // 设置附件上传大小
            $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
            $upload->savePath = './Public/upload/'; // 设置附件上传目录
            if (!$upload->upload()) {// 上传错误提示错误信息
                $this->error($upload->getErrorMsg());
            } else {// 上传成功 获取上传文件信息
                $info = $upload->getUploadFileInfo();
            }
            // 保存表单数据 包括附件数据
            $news->logo_img = $info[0]['savename'];  // 保存上传的照片根据需要自行组装
        }

        // 保存表单数据 包括附件数据
        $news->title = $_POST['title'];
        $news->content = $_POST['content'];
        $news->intro = $_POST['intro'];
        $news->editor = $_POST['editor'];
        $news->originate = $_POST['originate'];
        $news->flag = $_POST['flag'];
        $news->shei = $_POST['shei'];
        $news->sort = $_POST['sort'];
        $news->hy = $_POST['hy'];
        $news->time = date('Y-m-d H:i:s');
        $news->add(); // 写入用户数据到数据库
        $this->success('数据保存成功！', 'xwgl');
    }

}